<template>
  <div class="bigbox">
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="100px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="园区/建筑群">
                  <FilterSelect2
                      v-model="orgname"
                      :optionlist="qiyelist"
                      @updateModelValue="changeqiye"
                      :valuekey="{ name: 'name', value: 'id' }"
                    ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="19" style="text-align: right;">
                <el-form-item label="">
                  <el-button type="primary" size="small" @click="gettablelist()">查 询</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div>
    <div class="bodybox">
      <div style="display: flex;justify-content: space-between;">
        <div>建行维护</div>
        <div class="button"><el-button @click="baocuo" type="primary" size="small">保 存</el-button></div>
      </div>
     <!-- 表格区域 -->
     <div class="table">
       <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" >
        <el-table-column
            prop="address"
            label="用户建行开户名称">
            <template #default="{row}">
              <el-input v-model="row.ccbName" size="small"></el-input>
            </template>
        </el-table-column>
        <el-table-column
            prop="address"
            label="用户建行开户账号">
            <template #default="{row}">
              <el-input v-model="row.ccbAccount" size="small"></el-input>
            </template>
        </el-table-column>
       </TableColumn>
        <!-- 底部分页 -->
        <div class="fenye">
          <el-pagination
                background
                layout="prev, pager, next"
                :current-page.sync="page.pageNo"
                :page-size="15"
                @current-change="gettablelist"
                :total="+totalCount">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import TableColumn from '@/components/Table/index.vue'
import { getloginlistapi } from '@/api/prwpaid/file-management/Architectural-complex'
import {gettablelistapi,postccbapi} from '@/api/prwpaid/configura/ccb.js'
export default {
  name: 'CallPoliceIndex',
  components: {
    TableColumn
  },
  data() {
    return {
      orgname:'',
      qiyelist: [],
      tableData: [],
      tableConfig: [
      { label: '序号', type:'index' ,center:'center',width:'100'},
        { label: '建筑群', prop: 'buildingName' },
        { label: '用户名称', prop: 'customerName' },
        { label: '用户状态', prop: 'state' },
      ],
      totalCount: '',
      page: {
        pageNo: 0,
        pageSize:15
      }
    };
  },

  async mounted() {
    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(item => item.id == localbuildingId);
    console.log(isBuildIdInArray,localbuildingId);
    if(isBuildIdInArray){
      this.orgname=localbuildingId?localbuildingId:''
    }else{
      this.orgname=''
    }
    this.gettablelist()
  },

  methods: {
    // 企业下拉
    changeqiye(val) {
      localStorage.setItem('buildingId', val)
    },
    // 获取表格数据
    async gettablelist() {
      this.$refs.tabledata.loading=true
      const res = await gettablelistapi({ buildingId: this.orgname, pageNo: this.page.pageNo - 1, pageSize: this.page.pageSize })
      this.tableData = res.data.list
      this.totalCount=res.data.totalCount
      this.$refs.tabledata.loading=false
    },
    // 保存
    async baocuo() {
      const array = this.tableData.map(ele => {
        return {
          customerId: ele.customerId,
          ccbAccount: ele.ccbAccount,
          ccbName: ele.ccbName,
          id:ele.id
        }
      })
      await postccbapi(array)
      this.$message.success('操作成功')
      this.gettablelist()
    }
  },
};
</script>

<style lang="scss" scoped>
 .option{
  padding-left:10px;
 }
 .bigbox{
  height: 100%;
  padding: 10px;
  .topbox{
    padding:20px;
    height: 80px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    background-color: #fff;
    padding: 20px;
    .button{
      text-align: right;
      margin-bottom: 10px;
    }
    .table{
      height: 88%;
    }
    .fenye{
      text-align: right;
      margin-top: 10px;
    }
  }
 }
</style>